﻿Imports QLPhuTungOto.Data
Imports QLPhuTungOto.Bus
Imports DevExpress.XtraEditors
Public Class ucQLHoaDon
    Dim hdBUS As New HoaDonBUS
    Dim cthdBUS As New ChiTietHoaDon
    Dim TrangHienTai As Integer = 1
    Dim TongSoTrang As Integer = 1
    Dim SoDongTrenTrang As Integer = 0

    Private Sub ucQLHoaDon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UcPhanTrang1.txtSoDongCuaTrang.Text = HamHoTro.DocThongSo("config.config", "PhanTrangHoaDon", "200")
        SoDongTrenTrang = Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text)
        'Dim lstHoaDon As New List(Of HoaDon)
        'lstHoaDon = hdBUS.LayBang()
        'gcHoaDon.DataSource = hdBUS.LayHoaDon()
        
    End Sub

    Private Sub LoadDSHDTheoThoiGian()
        SoDongTrenTrang = Integer.Parse(Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text))
        gcHoaDon.DataSource = hdBUS.LayHoaDonTheoThoiGian(_from, _to, (TrangHienTai - 1) * SoDongTrenTrang, SoDongTrenTrang)
        UcPhanTrang1.lblTrangHienTai.Text = TrangHienTai.ToString + "/" + TongSoTrang.ToString
    End Sub

    Public Sub LoadChiTiet(ByVal ma As Integer)
        gcSanPham.DataSource = cthdBUS.LayChiTietHoaDon(ma)

    End Sub

    Private Sub gvHoaDon_FocusedRowChanged(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles gvHoaDon.FocusedRowChanged

        Dim i As Integer = gvHoaDon.FocusedRowHandle
        txtMa.Text = gvHoaDon.GetRowCellValue(i, "MaHoaDon")
        txtNgayXuat.Text = gvHoaDon.GetRowCellValue(i, "NgayXuat")
        txtTongTien.Text = gvHoaDon.GetRowCellValue(i, "TongTien")
        txtGhiChu.Text = gvHoaDon.GetRowCellValue(i, "GhiChu")
        If gvHoaDon.RowCount > 0 Then
            LoadChiTiet(Integer.Parse(gvHoaDon.GetRowCellValue(i, "MaHoaDon")))
        Else
            gcSanPham.DataSource = Nothing
        End If


    End Sub

    Dim _from As Date
    Dim _to As Date
    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        _from = dnFrom.DateTime
        _to = dnTo.DateTime

        Dim temp As Date
        If _from > _to Then
            temp = _to
            _to = _from
            _from = temp
        End If

        TinhSoTrang()
        LoadDSHDTheoThoiGian()
       
        'gcHoaDon.DataSource = hdBUS.LayHoaDonTheoThoiGian(_from, _to)
    End Sub

    Private Sub CapNhatStatus()
        ToolStripStatusLabel2.Text = dnFrom.DateTime.ToShortDateString()
        ToolStripStatusLabel4.Text = dnTo.DateTime.ToShortDateString() + ": "
        ToolStripStatusLabel5.Text = gvHoaDon.RowCount.ToString()
    End Sub

    Private Sub gvHoaDon_DataSourceChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvHoaDon.DataSourceChanged
        CapNhatStatus()
    End Sub

    Private Sub UcPhanTrang1_SuaSoDongTrenTrang() Handles UcPhanTrang1.SuaSoDongTrenTrang
        Try
            SoDongTrenTrang = Integer.Parse(UcPhanTrang1.txtSoDongCuaTrang.Text)

            If SoDongTrenTrang > 0 Then
                TinhSoTrang()
                TrangHienTai = 1
                LoadDSHDTheoThoiGian()
                HamHoTro.GhiThongSo("config.config", "PhanTrangHoaDon", UcPhanTrang1.txtSoDongCuaTrang.Text.Trim)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangCuoi_Click() Handles UcPhanTrang1.TrangCuoi_Click
        Try
            TrangHienTai = TongSoTrang
            LoadDSHDTheoThoiGian()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangDau_Click() Handles UcPhanTrang1.TrangDau_Click
        Try
            TrangHienTai = 1
            LoadDSHDTheoThoiGian()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangSau_Click() Handles UcPhanTrang1.TrangSau_Click
        Try
            If TrangHienTai < TongSoTrang Then
                TrangHienTai += 1
                LoadDSHDTheoThoiGian()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub UcPhanTrang1_TrangTruoc_Click() Handles UcPhanTrang1.TrangTruoc_Click
        Try
            If TrangHienTai > 1 Then
                TrangHienTai -= 1
                LoadDSHDTheoThoiGian()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function TinhSoTrang() As Integer
        Dim st As Integer = 0
        Try

            Dim soluonghd As Integer = hdBUS.DemHoaDonTheoThoiGian(_from, _to)
            st = (soluonghd \ SoDongTrenTrang)

            If soluonghd Mod SoDongTrenTrang <> 0 Then
                st += 1
            End If

            TongSoTrang = st

        Catch ex As Exception

        End Try
        Return st
    End Function
   
End Class
